/**
 * Copyright 2007 American Well Systems
 * All rights reserved.
 *
 * It is illegal to use, reproduce or distribute
 * any part of this Intellectual Property without
 * prior written authorization from American Well.
 */
package com.primera.geneiousplugin.ui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * The dialog that is displayed after the operation completes.
 * Displays the results to the user.
 * @author Rich O'Connell
 */
public class CompletionDialog extends JDialog implements Console {
	private static final long serialVersionUID = -2109481252631943620L;

	private JTextArea output;
	private JLabel docsCreated;

	/**
	 * Constructo.
	 */
	public CompletionDialog() {
		super((Frame)null, ResourceManager.getMsg("dialog.complete.title"), true);
		init();
	}

	protected void init() {
		setSize(new Dimension(350, 300));

		output = new JTextArea(5, 30);
		output.setEditable(false);

		JPanel mainPanel = new JPanel(new GridBagLayout());
		add(mainPanel);

		GridBagConstraints gbc = new GridBagConstraints();
		gbc.insets = new Insets(5,5,5,5);
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.anchor = GridBagConstraints.LINE_START;


		mainPanel.add(docsCreated = new JLabel(""), gbc);
		gbc.gridy = 1;
		mainPanel.add(new JLabel(ResourceManager.getMsg("dialog.complete.out")), gbc);



		JButton btnOk = new JButton(ResourceManager.getMsg("dialog.complete.ok"));
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompletionDialog.this.dispose();
			}
		});
		gbc.gridy = 3;
		gbc.anchor = GridBagConstraints.CENTER;
		mainPanel.add(btnOk, gbc);

		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1;
		gbc.weighty = 1;
		mainPanel.add(new JScrollPane(output), gbc);

	}

	/**
	 * Set the number of documents successfully created.
	 * @param docs
	 */
	public void setDocumentsCreated(int docs) {
		docsCreated.setText(ResourceManager.getMsg("dialog.complete.created") + ": " + docs);
	}

	/**
	 * {@inheritDoc}
	 */
	public void write(String message) {
		if (output.getText() != null && output.getText().length() != 0) {
			output.setText(output.getText() + "\n" + message);
		}
		else {
			output.setText(message);
		}
	}

}
